package ru.electron.menu.fragments;


import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import ru.electron.menu.R;
import ru.electron.menu.activities.DishListActivity;
import ru.electron.menu.provider.MenuProvider;

public class TagListFragment extends AbstractListFragment {


    private Cursor c;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        c.moveToPosition(position);
        int tagsId = c.getInt(c.getColumnIndex(BaseColumns._ID));
        Intent intent = new Intent(getActivity(), DishListActivity.class);
        intent.putExtra(DishListActivity.KEY_TAG, tagsId);
        startActivity(intent);
    }

    @Override
    public String getHeader() {
        return getString(R.string.categories);
    }

    @Override
    public ListAdapter getListAdapter() {
        c = getActivity().managedQuery(MenuProvider.URI_TAGS, null, null, null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item, c, new String[]{MenuProvider.KEY_TAG_NAME}, new int[]{R.id.name},
                CursorAdapter.FLAG_AUTO_REQUERY);
        return adapter;
    }
}
